#include "WidgetSessionStatus.hpp"


WidgetSessionStatus::WidgetSessionStatus(const char *pRootPath, const char *pIniUIFilename, const char *pIniDriverMappingFilename, const char *pIniVehiculeClassMappingFilename)
{
	initialized = false;
	rootPath = pRootPath; 

	nbProfiles = GetPrivateProfileInt("Profiles", "count", 0, pIniUIFilename);
	for (int i = 0 ; i < nbProfiles; i++) {
		sprintf(tmp, "Profile.%d", (i+1));
		profiles[i].widgetOffsetX = (signed int)GetPrivateProfileInt(tmp, "SessionStatus.offsetX", 0, pIniUIFilename);
		profiles[i].widgetOffsetY = (signed int)GetPrivateProfileInt(tmp, "SessionStatus.offsetY", 0, pIniUIFilename);
		profiles[i].hide = GetPrivateProfileInt(tmp, "SessionStatus.hide", 0, pIniUIFilename) != 0;
	}

	//std::string str = rootPath;
	GetPrivateProfileString("UI Widgets", "SessionStatus.ini", "", tmp, 999, pIniUIFilename);
	//str.append(tmp);
	sprintf(iniUIFilename, "%s%s", rootPath, tmp);
	//iniUIFilename = str.c_str(); //pIniUIFilename; 
	available = GetPrivateProfileInt("Session Status", "available", 0, iniUIFilename) != 0;
}

void WidgetSessionStatus::init(const ScreenInfoV01 &info, float ratio, float ratioWidth, float ratioHeight, Sprite spriteList[]) {
	if (!available) {
		initialized = true;
		return;
	}

	for (int i = 0 ; i < nbProfiles; i++) {
		profiles[i].widgetOffsetX *= ratioWidth;
		profiles[i].widgetOffsetY *= ratioHeight;
	}

	int posX = (int)(ratioWidth * GetPrivateProfileInt("Session Status", "image.left", 40, iniUIFilename));//40;
	int posY = (int)(ratioHeight * GetPrivateProfileInt("Session Status", "image.top", 550, iniUIFilename));//550;

	//LPDIRECT3DDEVICE9 dev = static_cast<LPDIRECT3DDEVICE9>(info.mDevice);	
	

	bgImageData.spriteIndex = GetPrivateProfileInt("Session Status", "image.id", 0, iniUIFilename) -1;
	if (bgImageData.spriteIndex > -1) {
		bgImageData.left = posX;
		bgImageData.top = posY;
		bgImageData.width = (int)(GetPrivateProfileInt("Session Status", "image.width", (int)spriteList[bgImageData.spriteIndex].width, iniUIFilename) * ratioWidth);
		bgImageData.height = (int)(GetPrivateProfileInt("Session Status", "image.height", (int)spriteList[bgImageData.spriteIndex].height, iniUIFilename) * ratioHeight);
		bgImageData.colorIndex = GetPrivateProfileInt("Session Status", "image.color.id", 1, iniUIFilename) -1;
	}

	infoAvailable = GetPrivateProfileInt("Session Status.Info", "available", 0, iniUIFilename) != 0;
	if (infoAvailable) {
		infoTextData.fontIndex = GetPrivateProfileInt("Session Status.Info", "text.font.id", 1, iniUIFilename) - 1;
		infoTextData.colorIndex = GetPrivateProfileInt("Session Status.Info", "text.font.color.id", 1, iniUIFilename) - 1;
		infoTextData.rect.left = (int)(posX + GetPrivateProfileInt("Session Status.Info", "text.left", 0, iniUIFilename) * ratioWidth);
		infoTextData.rect.top = (int)(posY + GetPrivateProfileInt("Session Status.Info", "text.top", 0, iniUIFilename) * ratioHeight);
		infoTextData.rect.right = (int)(posX + GetPrivateProfileInt("Session Status.Info", "text.right", 100, iniUIFilename) * ratioWidth);
		infoTextData.rect.bottom = (int)(posY + GetPrivateProfileInt("Session Status.Info", "text.bottom", 100, iniUIFilename) * ratioHeight);
		infoTextData.alignment = GetPrivateProfileInt("Session Status.Info", "text.alignment", 1, iniUIFilename);
	}

	//////////////////////////////////
	// S1 Flags
	//////////////////////////////////
	s1NoFlagAvailable = GetPrivateProfileInt("Session Status.S1.NoFlag", "available", 0, iniUIFilename) != 0;
	//if (s1NoFlagAvailable) {
		s1NoFlagImageData.spriteIndex = GetPrivateProfileInt("Session Status.S1.NoFlag", "image.id", 0, iniUIFilename) -1;
		s1NoFlagImageData.left = (int)(posX + GetPrivateProfileInt("Session Status.S1.NoFlag", "image.left", 0, iniUIFilename) * ratioWidth);
		s1NoFlagImageData.top = (int)(posY + GetPrivateProfileInt("Session Status.S1.NoFlag", "image.top", 0, iniUIFilename) * ratioHeight);
		s1NoFlagImageData.colorIndex = GetPrivateProfileInt("Session Status.S1.NoFlag", "image.color.id", 1, iniUIFilename) -1;
		if (s1NoFlagImageData.spriteIndex != -1) {
			s1NoFlagImageData.width = (int)(GetPrivateProfileInt("Session Status.S1.NoFlag", "image.width", (int)spriteList[s1NoFlagImageData.spriteIndex].width, iniUIFilename) * ratioWidth);
			s1NoFlagImageData.height = (int)(GetPrivateProfileInt("Session Status.S1.NoFlag", "image.height", (int)spriteList[s1NoFlagImageData.spriteIndex].height, iniUIFilename) * ratioHeight);
		}
	//}

	s1YellowFlagAvailable =  GetPrivateProfileInt("Session Status.S1.YellowFlag", "available", 0, iniUIFilename) != 0;
	if (s1YellowFlagAvailable) {
		s1YellowFlagImageData.spriteIndex = GetPrivateProfileInt("Session Status.S1.YellowFlag", "image.id", 0, iniUIFilename) -1;
		if (s1YellowFlagImageData.spriteIndex != -1) {
			s1YellowFlagImageData.colorIndex = GetPrivateProfileInt("Session Status.S1.YellowFlag", "image.color.id", 1, iniUIFilename) -1;
			s1YellowFlagImageData.left = (int)(posX + GetPrivateProfileInt("Session Status.S1.YellowFlag", "image.left", -9999, iniUIFilename) * ratioWidth);
			s1YellowFlagImageData.top = (int)(posY + GetPrivateProfileInt("Session Status.S1.YellowFlag", "image.top", -9999, iniUIFilename) * ratioHeight);
			if (s1YellowFlagImageData.left < 0) s1YellowFlagImageData.left = s1NoFlagImageData.left;
			if (s1YellowFlagImageData.top < 0) s1YellowFlagImageData.top = s1NoFlagImageData.top;
			s1YellowFlagImageData.width = (int)(GetPrivateProfileInt("Session Status.S1.YellowFlag", "image.width", (int)spriteList[s1YellowFlagImageData.spriteIndex].width, iniUIFilename) * ratioWidth);
			s1YellowFlagImageData.height = (int)(GetPrivateProfileInt("Session Status.S1.YellowFlag", "image.height", (int)spriteList[s1YellowFlagImageData.spriteIndex].height, iniUIFilename) * ratioHeight);
		}

	}

	s1FinishFlagAvailable =  GetPrivateProfileInt("Session Status.S1.FinishFlag", "available", 0, iniUIFilename) != 0;
	if (s1FinishFlagAvailable) {
		s1FinishFlagImageData.spriteIndex = GetPrivateProfileInt("Session Status.S1.FinishFlag", "image.id", 0, iniUIFilename) -1;
		if (s1FinishFlagImageData.spriteIndex != -1) {
			s1FinishFlagImageData.colorIndex = GetPrivateProfileInt("Session Status.S1.FinishFlag", "image.color.id", 1, iniUIFilename) -1;
			s1FinishFlagImageData.left = (int)(posX + GetPrivateProfileInt("Session Status.S1.FinishFlag", "image.left", -9999, iniUIFilename) * ratioWidth);
			s1FinishFlagImageData.top = (int)(posY + GetPrivateProfileInt("Session Status.S1.FinishFlag", "image.top", -9999, iniUIFilename) * ratioHeight);
			if (s1FinishFlagImageData.left < 0) s1FinishFlagImageData.left = s1NoFlagImageData.left;
			if (s1FinishFlagImageData.top < 0) s1FinishFlagImageData.top = s1NoFlagImageData.top;
			s1FinishFlagImageData.width = (int)(GetPrivateProfileInt("Session Status.S1.FinishFlag", "image.width", (int)spriteList[s1FinishFlagImageData.spriteIndex].width, iniUIFilename) * ratioWidth);
			s1FinishFlagImageData.height = (int)(GetPrivateProfileInt("Session Status.S1.FinishFlag", "image.height", (int)spriteList[s1FinishFlagImageData.spriteIndex].height, iniUIFilename) * ratioHeight);
		}

	}

	//////////////////////////////////
	// S2 Flags
	//////////////////////////////////
	s2NoFlagAvailable = GetPrivateProfileInt("Session Status.S2.NoFlag", "available", 0, iniUIFilename) != 0;
	//if (s2NoFlagAvailable) {
		s2NoFlagImageData.spriteIndex = GetPrivateProfileInt("Session Status.S2.NoFlag", "image.id", 0, iniUIFilename) -1;
		s2NoFlagImageData.left = (int)(posX + GetPrivateProfileInt("Session Status.S2.NoFlag", "image.left", 0, iniUIFilename) * ratioWidth);
		s2NoFlagImageData.top = (int)(posY + GetPrivateProfileInt("Session Status.S2.NoFlag", "image.top", 0, iniUIFilename) * ratioHeight);
		s2NoFlagImageData.colorIndex = GetPrivateProfileInt("Session Status.S2.NoFlag", "image.color.id", 1, iniUIFilename) -1;
		if (s2NoFlagImageData.spriteIndex != -1) {
			s2NoFlagImageData.width = (int)(GetPrivateProfileInt("Session Status.S2.NoFlag", "image.width", (int)spriteList[s2NoFlagImageData.spriteIndex].width, iniUIFilename) * ratioWidth);
			s2NoFlagImageData.height = (int)(GetPrivateProfileInt("Session Status.S2.NoFlag", "image.height", (int)spriteList[s2NoFlagImageData.spriteIndex].height, iniUIFilename) * ratioHeight);
		}
	//}

	s2YellowFlagAvailable =  GetPrivateProfileInt("Session Status.S2.YellowFlag", "available", 0, iniUIFilename) != 0;
	if (s2YellowFlagAvailable) {
		s2YellowFlagImageData.spriteIndex = GetPrivateProfileInt("Session Status.S2.YellowFlag", "image.id", 0, iniUIFilename) -1;
		if (s2YellowFlagImageData.spriteIndex != -1) {
			s2YellowFlagImageData.colorIndex = GetPrivateProfileInt("Session Status.S2.YellowFlag", "image.color.id", 1, iniUIFilename) -1;
			s2YellowFlagImageData.left = (int)(posX + GetPrivateProfileInt("Session Status.S2.YellowFlag", "image.left", -9999, iniUIFilename) * ratioWidth);
			s2YellowFlagImageData.top = (int)(posY + GetPrivateProfileInt("Session Status.S2.YellowFlag", "image.top", -9999, iniUIFilename) * ratioHeight);
			if (s2YellowFlagImageData.left < 0) s2YellowFlagImageData.left = s2NoFlagImageData.left;
			if (s2YellowFlagImageData.top < 0) s2YellowFlagImageData.top = s2NoFlagImageData.top;
			s2YellowFlagImageData.width = (int)(GetPrivateProfileInt("Session Status.S2.YellowFlag", "image.width", (int)spriteList[s2YellowFlagImageData.spriteIndex].width, iniUIFilename) * ratioWidth);
			s2YellowFlagImageData.height = (int)(GetPrivateProfileInt("Session Status.S2.YellowFlag", "image.height", (int)spriteList[s2YellowFlagImageData.spriteIndex].height, iniUIFilename) * ratioHeight);
		}

	}

	s2FinishFlagAvailable =  GetPrivateProfileInt("Session Status.S2.FinishFlag", "available", 0, iniUIFilename) != 0;
	if (s2FinishFlagAvailable) {
		s2FinishFlagImageData.spriteIndex = GetPrivateProfileInt("Session Status.S2.FinishFlag", "image.id", 0, iniUIFilename) -1;
		if (s2FinishFlagImageData.spriteIndex != -1) {
			s2FinishFlagImageData.colorIndex = GetPrivateProfileInt("Session Status.S2.FinishFlag", "image.color.id", 1, iniUIFilename) -1;
			s2FinishFlagImageData.left = (int)(posX + GetPrivateProfileInt("Session Status.S2.FinishFlag", "image.left", -9999, iniUIFilename) * ratioWidth);
			s2FinishFlagImageData.top = (int)(posY + GetPrivateProfileInt("Session Status.S2.FinishFlag", "image.top", -9999, iniUIFilename) * ratioHeight);
			if (s2FinishFlagImageData.left < 0) s2FinishFlagImageData.left = s2NoFlagImageData.left;
			if (s2FinishFlagImageData.top < 0) s2FinishFlagImageData.top = s2NoFlagImageData.top;
			s2FinishFlagImageData.width = (int)(GetPrivateProfileInt("Session Status.S2.FinishFlag", "image.width", (int)spriteList[s2FinishFlagImageData.spriteIndex].width, iniUIFilename) * ratioWidth);
			s2FinishFlagImageData.height = (int)(GetPrivateProfileInt("Session Status.S2.FinishFlag", "image.height", (int)spriteList[s2FinishFlagImageData.spriteIndex].height, iniUIFilename) * ratioHeight);
		}

	}

	//////////////////////////////////
	// S3 Flags
	//////////////////////////////////
	s3NoFlagAvailable = GetPrivateProfileInt("Session Status.S3.NoFlag", "available", 0, iniUIFilename) != 0;
	//if (s3NoFlagAvailable) {
		s3NoFlagImageData.spriteIndex = GetPrivateProfileInt("Session Status.S3.NoFlag", "image.id", 0, iniUIFilename) -1;
		s3NoFlagImageData.left = (int)(posX + GetPrivateProfileInt("Session Status.S3.NoFlag", "image.left", 0, iniUIFilename) * ratioWidth);
		s3NoFlagImageData.top = (int)(posY + GetPrivateProfileInt("Session Status.S3.NoFlag", "image.top", 0, iniUIFilename) * ratioHeight);
		s3NoFlagImageData.colorIndex = GetPrivateProfileInt("Session Status.S3.NoFlag", "image.color.id", 1, iniUIFilename) -1;
		if (s3NoFlagImageData.spriteIndex != -1) {
			s3NoFlagImageData.width = (int)(GetPrivateProfileInt("Session Status.S3.NoFlag", "image.width", (int)spriteList[s3NoFlagImageData.spriteIndex].width, iniUIFilename) * ratioWidth);
			s3NoFlagImageData.height = (int)(GetPrivateProfileInt("Session Status.S3.NoFlag", "image.height", (int)spriteList[s3NoFlagImageData.spriteIndex].height, iniUIFilename) * ratioHeight);
		}
	//}

	s3YellowFlagAvailable =  GetPrivateProfileInt("Session Status.S3.YellowFlag", "available", 0, iniUIFilename) != 0;
	if (s3YellowFlagAvailable) {
		s3YellowFlagImageData.spriteIndex = GetPrivateProfileInt("Session Status.S3.YellowFlag", "image.id", 0, iniUIFilename) -1;
		if (s3YellowFlagImageData.spriteIndex != -1) {
			s3YellowFlagImageData.colorIndex = GetPrivateProfileInt("Session Status.S3.YellowFlag", "image.color.id", 1, iniUIFilename) -1;
			s3YellowFlagImageData.left = (int)(posX + GetPrivateProfileInt("Session Status.S3.YellowFlag", "image.left", -9999, iniUIFilename) * ratioWidth);
			s3YellowFlagImageData.top = (int)(posY + GetPrivateProfileInt("Session Status.S3.YellowFlag", "image.top", -9999, iniUIFilename) * ratioHeight);
			if (s3YellowFlagImageData.left < 0) s3YellowFlagImageData.left = s3NoFlagImageData.left;
			if (s3YellowFlagImageData.top < 0) s3YellowFlagImageData.top = s3NoFlagImageData.top;
			s3YellowFlagImageData.width = (int)(GetPrivateProfileInt("Session Status.S3.YellowFlag", "image.width", (int)spriteList[s3YellowFlagImageData.spriteIndex].width, iniUIFilename) * ratioWidth);
			s3YellowFlagImageData.height = (int)(GetPrivateProfileInt("Session Status.S3.YellowFlag", "image.height", (int)spriteList[s3YellowFlagImageData.spriteIndex].height, iniUIFilename) * ratioHeight);
		}

	}

	s3FinishFlagAvailable =  GetPrivateProfileInt("Session Status.S3.FinishFlag", "available", 0, iniUIFilename) != 0;
	if (s3FinishFlagAvailable) {
		s3FinishFlagImageData.spriteIndex = GetPrivateProfileInt("Session Status.S3.FinishFlag", "image.id", 0, iniUIFilename) -1;
		if (s3FinishFlagImageData.spriteIndex != -1) {
			s3FinishFlagImageData.colorIndex = GetPrivateProfileInt("Session Status.S3.FinishFlag", "image.color.id", 1, iniUIFilename) -1;
			s3FinishFlagImageData.left = (int)(posX + GetPrivateProfileInt("Session Status.S3.FinishFlag", "image.left", -9999, iniUIFilename) * ratioWidth);
			s3FinishFlagImageData.top = (int)(posY + GetPrivateProfileInt("Session Status.S3.FinishFlag", "image.top", -9999, iniUIFilename) * ratioHeight);
			if (s3FinishFlagImageData.left < 0) s3FinishFlagImageData.left = s3NoFlagImageData.left;
			if (s3FinishFlagImageData.top < 0) s3FinishFlagImageData.top = s3NoFlagImageData.top;
			s3FinishFlagImageData.width = (int)(GetPrivateProfileInt("Session Status.S3.FinishFlag", "image.width", (int)spriteList[s3FinishFlagImageData.spriteIndex].width, iniUIFilename) * ratioWidth);
			s3FinishFlagImageData.height = (int)(GetPrivateProfileInt("Session Status.S3.FinishFlag", "image.height", (int)spriteList[s3FinishFlagImageData.spriteIndex].height, iniUIFilename) * ratioHeight);
		}

	}

	//////////////////////////////////
	// Lights
	//////////////////////////////////
	lightOffAvailable = GetPrivateProfileInt("Session Status.NoLight", "available", 0, iniUIFilename) != 0;
	//if (lightOffAvailable) {
		lightOffImageData.spriteIndex = GetPrivateProfileInt("Session Status.NoLight", "image.id", 0, iniUIFilename) -1;
		lightOffImageData.left = (int)(posX + GetPrivateProfileInt("Session Status.NoLight", "image.left", 0, iniUIFilename) * ratioWidth);
		lightOffImageData.top = (int)(posY + GetPrivateProfileInt("Session Status.NoLight", "image.top", 0, iniUIFilename) * ratioHeight);
		lightOffImageData.colorIndex = GetPrivateProfileInt("Session Status.NoLight", "image.color.id", 1, iniUIFilename) -1;
		if (lightOffImageData.spriteIndex != -1) {
			lightOffImageData.width = (int)(GetPrivateProfileInt("Session Status.NoLight", "image.width", (int)spriteList[lightOffImageData.spriteIndex].width, iniUIFilename) * ratioWidth);
			lightOffImageData.height = (int)(GetPrivateProfileInt("Session Status.NoLight", "image.height", (int)spriteList[lightOffImageData.spriteIndex].height, iniUIFilename) * ratioHeight);
		}
	//}

	lightRedAvailable =  GetPrivateProfileInt("Session Status.RedLight", "available", 0, iniUIFilename) != 0;
	if (lightRedAvailable) {
		lightRedImageData.spriteIndex = GetPrivateProfileInt("Session Status.RedLight", "image.id", 0, iniUIFilename) -1;
		if (lightRedImageData.spriteIndex != -1) {
			lightRedImageData.colorIndex = GetPrivateProfileInt("Session Status.RedLight", "image.color.id", 1, iniUIFilename) -1;
			lightRedImageData.left = (int)(posX + GetPrivateProfileInt("Session Status.RedLight", "image.left", -9999, iniUIFilename) * ratioWidth);
			lightRedImageData.top = (int)(posY + GetPrivateProfileInt("Session Status.RedLight", "image.top", -9999, iniUIFilename) * ratioHeight);
			if (lightRedImageData.left < 0) lightRedImageData.left = lightOffImageData.left;
			if (lightRedImageData.top < 0) lightRedImageData.top = lightOffImageData.top;
			lightRedImageData.width = (int)(GetPrivateProfileInt("Session Status.RedLight", "image.width", (int)spriteList[lightRedImageData.spriteIndex].width, iniUIFilename) * ratioWidth);
			lightRedImageData.height = (int)(GetPrivateProfileInt("Session Status.RedLight", "image.height", (int)spriteList[lightRedImageData.spriteIndex].height, iniUIFilename) * ratioHeight);
		}

	}

	lightGreenAvailable =  GetPrivateProfileInt("Session Status.GreenLight", "available", 0, iniUIFilename) != 0;
	if (lightGreenAvailable) {
		lightGreenImageData.spriteIndex = GetPrivateProfileInt("Session Status.GreenLight", "image.id", 0, iniUIFilename) -1;
		if (lightGreenImageData.spriteIndex != -1) {
			lightGreenImageData.colorIndex = GetPrivateProfileInt("Session Status.GreenLight", "image.color.id", 1, iniUIFilename) -1;
			lightGreenImageData.left = (int)(posX + GetPrivateProfileInt("Session Status.GreenLight", "image.left", -9999, iniUIFilename) * ratioWidth);
			lightGreenImageData.top = (int)(posY + GetPrivateProfileInt("Session Status.GreenLight", "image.top", -9999, iniUIFilename) * ratioHeight);
			if (lightGreenImageData.left < 0) lightGreenImageData.left = lightOffImageData.left;
			if (lightGreenImageData.top < 0) lightGreenImageData.top = lightOffImageData.top;
			lightGreenImageData.width = (int)(GetPrivateProfileInt("Session Status.GreenLight", "image.width", (int)spriteList[lightGreenImageData.spriteIndex].width, iniUIFilename) * ratioWidth);
			lightGreenImageData.height = (int)(GetPrivateProfileInt("Session Status.GreenLight", "image.height", (int)spriteList[lightGreenImageData.spriteIndex].height, iniUIFilename) * ratioHeight);
		}

	}


}

bool WidgetSessionStatus::isLapSessionLimit(const ScoringInfoV01 &info, DriverInfo & di) { 
		int maxLaps = info.mMaxLaps;
        if ( maxLaps >  1000000000 )
            maxLaps = 0;
		float endTime = info.mEndET;
        
        if ( ( maxLaps > 0 ) && ( maxLaps < 10000 ) )
        {
            if ( ( endTime > 0 ) && ( endTime < 999999 ) )
            {
				double avgLaptime = di.averageLapTime; // TODO: may be we need to compute a real average laptime (Inlaps, outlaps and laps slower than (1.06 * fastest) should be ignored.)
                if (avgLaptime <= 0) return true;

                int timeLaps = (int)( endTime / avgLaptime);
                
                if ( timeLaps < maxLaps )
                    return ( false);
            }
            
            return ( true);
        }
        
        if ( ( endTime > 0 ) && ( endTime < 999999 ) )
            return ( false);
        
        return ( true );
}

void WidgetSessionStatus::updateData(bool isRace, const ScoringInfoV01 &info, DriverInfo & viewedDriverInfo, DriverInfo orderedDriverInfos[]) {
	if (!available) return;
	

	DriverInfo & di = (isRace)?orderedDriverInfos[0]:viewedDriverInfo;
	
	isLapLimit = isLapSessionLimit(info, di);

	// Information
	if (info.mYellowFlagState == 5) {
		strcpy(infoStr , "Last Lap");
	} else if (!isLapLimit) {
		double seconds = info.mCurrentET;
		if (info.mGamePhase == 8) {
			if (di.finishStatus == 1) {
				strcpy(infoStr , "FINISHED");
			} else {
				strcpy(infoStr , "Last Lap");
			}
		} else if (isRace && (info.mGamePhase == 3 || info.mEndET < 0 || info.mEndET > 3000000)) {
			strcpy(infoStr , "-- : -- : --");
		} else if (info.mSession == 0 || info.mEndET < 0 || info.mEndET > 3000000) {
			strcpy(infoStr , getTimeAsString(seconds, true, true, false, true).c_str());
			//sprintf(infoStr, "-- : -- : --", info.mAmbientTemp);
		} else {
			float timeLeft = info.mEndET - di.lapStartET;
			int estimatedLapLeft = 1;
			if (di.averageLapTime > 0) { // TODO: may be we need to compute a real average laptime (Inlaps, outlaps and laps slower than (1.06 * fastest) should be ignored.)
				estimatedLapLeft = (int) (timeLeft / di.averageLapTime);
			}
			if (estimatedLapLeft <= 0) {// vsi.getCurrentLap() > 1 && vsi.getEstimatedMaxLaps() <= vsi.getCurrentLap()) {
				if (lastLapSwitchDisplay == 0) {
					lastLapSwitchDisplay = info.mCurrentET;
					lastLapDisplayed = true;
				} else {
					if (info.mCurrentET - lastLapSwitchDisplay > 2) {
						lastLapDisplayed = !lastLapDisplayed;
						lastLapSwitchDisplay = info.mCurrentET;
					}
				}
			} else {
				lastLapSwitchDisplay = 0;
				lastLapDisplayed = false;
			}
			if (lastLapDisplayed) {
				strcpy(infoStr , "Last Lap");
			} else {
				if (info.mEndET < info.mCurrentET) {
					strcpy(infoStr , "00 : 00 : 00");
				} else {
					strcpy(infoStr , getTimeAsString(info.mEndET - info.mCurrentET, true, true, false, true).c_str());
				}
			}
		}
	} else {
		int lap;
		if (isRace && info.mGamePhase == 3) {
			lap = 0;
		} else {
			lap = di.lapCompleted;
		}
		if (lap < 0) {
			lap = 0;
		}
		int maxLaps = info.mMaxLaps;
		if (di.lapCompleted == maxLaps - 1) {
			strcpy(infoStr , "Last Lap");
		} else if (di.lapCompleted >= maxLaps) {
			strcpy(infoStr , "FINISHED");
			//forceLightMode = LightMode.RED;
		} else {
			if (maxLaps < 10000) {
				sprintf(infoStr, "%d / %d" , lap, maxLaps);
			} else {
				sprintf(infoStr, "%d / --" , lap);
			}
		}
	}

	// Flag
	if (isRace && di.finishStatus == 1) {
		s1FlagToDisplay = 2;
		s2FlagToDisplay = 2;
		s3FlagToDisplay = 2;
	} else if (info.mGamePhase == 8) {
		s1FlagToDisplay = 2;
		s2FlagToDisplay = 2;
		s3FlagToDisplay = 2;
	} else {
		if (info.mSectorFlag[0] == 1) {
			s1FlagToDisplay = 1;
		} else {
			s1FlagToDisplay = 0;
		}
		if (info.mSectorFlag[1] == 1) {
			s2FlagToDisplay = 1;
		} else {
			s2FlagToDisplay = 0;
		}
		if (info.mSectorFlag[2] == 1) {
			s3FlagToDisplay = 1;
		} else {
			s3FlagToDisplay = 0;
		}
	}

	// Lights
	if (info.mGamePhase == 0) {
		lightToDisplay = 1;
	} else if (info.mStartLight > 0 && info.mStartLight <= info.mNumRedLights) {
		lightToDisplay = 1;
	} else if (info.mStartLight > info.mNumRedLights) {
		lightToDisplay = 2;
	} else if (di.finishStatus == 1) {
		lightToDisplay = 1;
	} else {
		lightToDisplay = 0;
	}

}

void WidgetSessionStatus::render(int profileIndex, const ScreenInfoV01 &info, bool isRace, const LPD3DXFONT *fontList, const D3DCOLOR *colorList, Sprite spriteList[], DriverInfo orderedDriverInfos[]) {
	if (!available) return;
		if (gReplayIsActive) return;
	if (profiles[profileIndex].hide) return;
		
	LPDIRECT3DDEVICE9 dev = static_cast<LPDIRECT3DDEVICE9>(info.mDevice);	
	if (bgImageData.spriteIndex > -1) {
		spriteList[bgImageData.spriteIndex].setPosition(bgImageData.left + profiles[profileIndex].widgetOffsetX, bgImageData.top + profiles[profileIndex].widgetOffsetY);
		spriteList[bgImageData.spriteIndex].setSize(bgImageData.width, bgImageData.height);
		spriteList[bgImageData.spriteIndex].render(dev, colorList[bgImageData.colorIndex], 100);
	}
	// Info
	if (infoAvailable) {
		tmpRect.left = infoTextData.rect.left + profiles[profileIndex].widgetOffsetX;
		tmpRect.top = infoTextData.rect.top + profiles[profileIndex].widgetOffsetY;
		tmpRect.right = infoTextData.rect.right + profiles[profileIndex].widgetOffsetX;
		tmpRect.bottom = infoTextData.rect.bottom + profiles[profileIndex].widgetOffsetY;
		fontList[infoTextData.fontIndex]->DrawTextA(NULL, infoStr, -1, &tmpRect, infoTextData.alignment, colorList[infoTextData.colorIndex]);
	}

	// S1 Flags
	if (s1FlagToDisplay == 0) {
		if (s1NoFlagAvailable && s1NoFlagImageData.spriteIndex != -1) {
			spriteList[s1NoFlagImageData.spriteIndex].setPosition(s1NoFlagImageData.left + profiles[profileIndex].widgetOffsetX, s1NoFlagImageData.top + profiles[profileIndex].widgetOffsetY);
			spriteList[s1NoFlagImageData.spriteIndex].setSize(s1NoFlagImageData.width, s1NoFlagImageData.height);
			spriteList[s1NoFlagImageData.spriteIndex].render(dev, colorList[s1NoFlagImageData.colorIndex], 100);
		}
	} else if (s1FlagToDisplay == 1) {
		if (s1YellowFlagAvailable && s1YellowFlagImageData.spriteIndex != -1) {
			spriteList[s1YellowFlagImageData.spriteIndex].setPosition(s1YellowFlagImageData.left + profiles[profileIndex].widgetOffsetX, s1YellowFlagImageData.top + profiles[profileIndex].widgetOffsetY);
			spriteList[s1YellowFlagImageData.spriteIndex].setSize(s1YellowFlagImageData.width, s1YellowFlagImageData.height);
			spriteList[s1YellowFlagImageData.spriteIndex].render(dev, colorList[s1YellowFlagImageData.colorIndex], 100);
		}
	} else if (s1FlagToDisplay == 2) {
		if (s1FinishFlagAvailable && s1FinishFlagImageData.spriteIndex != -1) {
			spriteList[s1FinishFlagImageData.spriteIndex].setPosition(s1FinishFlagImageData.left + profiles[profileIndex].widgetOffsetX, s1FinishFlagImageData.top + profiles[profileIndex].widgetOffsetY);
			spriteList[s1FinishFlagImageData.spriteIndex].setSize(s1FinishFlagImageData.width, s1FinishFlagImageData.height);
			spriteList[s1FinishFlagImageData.spriteIndex].render(dev, colorList[s1FinishFlagImageData.colorIndex], 100);
		}
	}

	// S2 Flags
	if (s2FlagToDisplay == 0) {
		if (s2NoFlagAvailable && s2NoFlagImageData.spriteIndex != -1) {
			spriteList[s2NoFlagImageData.spriteIndex].setPosition(s2NoFlagImageData.left + profiles[profileIndex].widgetOffsetX, s2NoFlagImageData.top + profiles[profileIndex].widgetOffsetY);
			spriteList[s2NoFlagImageData.spriteIndex].setSize(s2NoFlagImageData.width, s2NoFlagImageData.height);
			spriteList[s2NoFlagImageData.spriteIndex].render(dev, colorList[s2NoFlagImageData.colorIndex], 100);
		}
	} else if (s2FlagToDisplay == 1) {
		if (s2YellowFlagAvailable && s2YellowFlagImageData.spriteIndex != -1) {
			spriteList[s2YellowFlagImageData.spriteIndex].setPosition(s2YellowFlagImageData.left + profiles[profileIndex].widgetOffsetX, s2YellowFlagImageData.top + profiles[profileIndex].widgetOffsetY);
			spriteList[s2YellowFlagImageData.spriteIndex].setSize(s2YellowFlagImageData.width, s2YellowFlagImageData.height);
			spriteList[s2YellowFlagImageData.spriteIndex].render(dev, colorList[s2YellowFlagImageData.colorIndex], 100);
		}
	} else if (s2FlagToDisplay == 2) {
		if (s2FinishFlagAvailable && s2FinishFlagImageData.spriteIndex != -1) {
			spriteList[s2FinishFlagImageData.spriteIndex].setPosition(s2FinishFlagImageData.left + profiles[profileIndex].widgetOffsetX, s2FinishFlagImageData.top + profiles[profileIndex].widgetOffsetY);
			spriteList[s2FinishFlagImageData.spriteIndex].setSize(s2FinishFlagImageData.width, s2FinishFlagImageData.height);
			spriteList[s2FinishFlagImageData.spriteIndex].render(dev, colorList[s2FinishFlagImageData.colorIndex], 100);
		}
	}

	// S3 Flags
	if (s3FlagToDisplay == 0) {
		if (s3NoFlagAvailable && s3NoFlagImageData.spriteIndex != -1) {
			spriteList[s3NoFlagImageData.spriteIndex].setPosition(s3NoFlagImageData.left + profiles[profileIndex].widgetOffsetX, s3NoFlagImageData.top + profiles[profileIndex].widgetOffsetY);
			spriteList[s3NoFlagImageData.spriteIndex].setSize(s3NoFlagImageData.width, s3NoFlagImageData.height);
			spriteList[s3NoFlagImageData.spriteIndex].render(dev, colorList[s3NoFlagImageData.colorIndex], 100);
		}
	} else if (s3FlagToDisplay == 1) {
		if (s3YellowFlagAvailable && s3YellowFlagImageData.spriteIndex != -1) {
			spriteList[s3YellowFlagImageData.spriteIndex].setPosition(s3YellowFlagImageData.left + profiles[profileIndex].widgetOffsetX, s3YellowFlagImageData.top + profiles[profileIndex].widgetOffsetY);
			spriteList[s3YellowFlagImageData.spriteIndex].setSize(s3YellowFlagImageData.width, s3YellowFlagImageData.height);
			spriteList[s3YellowFlagImageData.spriteIndex].render(dev, colorList[s3YellowFlagImageData.colorIndex], 100);
		}
	} else if (s3FlagToDisplay == 2) {
		if (s3FinishFlagAvailable && s3FinishFlagImageData.spriteIndex != -1) {
			spriteList[s3FinishFlagImageData.spriteIndex].setPosition(s3FinishFlagImageData.left + profiles[profileIndex].widgetOffsetX, s3FinishFlagImageData.top + profiles[profileIndex].widgetOffsetY);
			spriteList[s3FinishFlagImageData.spriteIndex].setSize(s3FinishFlagImageData.width, s3FinishFlagImageData.height);
			spriteList[s3FinishFlagImageData.spriteIndex].render(dev, colorList[s3FinishFlagImageData.colorIndex], 100);
		}
	}

	// Lights
	if (lightToDisplay == 0) {
		if (lightOffAvailable && lightOffImageData.spriteIndex != -1) {
			spriteList[lightOffImageData.spriteIndex].setPosition(lightOffImageData.left + profiles[profileIndex].widgetOffsetX, lightOffImageData.top + profiles[profileIndex].widgetOffsetY);
			spriteList[lightOffImageData.spriteIndex].setSize(lightOffImageData.width, lightOffImageData.height);
			spriteList[lightOffImageData.spriteIndex].render(dev, colorList[lightOffImageData.colorIndex], 100);
		}
	} else if (lightToDisplay == 1) {
		if (lightRedAvailable && lightRedImageData.spriteIndex != -1) {
			spriteList[lightRedImageData.spriteIndex].setPosition(lightRedImageData.left + profiles[profileIndex].widgetOffsetX, lightRedImageData.top + profiles[profileIndex].widgetOffsetY);
			spriteList[lightRedImageData.spriteIndex].setSize(lightRedImageData.width, lightRedImageData.height);
			spriteList[lightRedImageData.spriteIndex].render(dev, colorList[lightRedImageData.colorIndex], 100);
		}
	} else if (lightToDisplay == 2) {
		if (lightGreenAvailable && lightGreenImageData.spriteIndex != -1) {
			spriteList[lightGreenImageData.spriteIndex].setPosition(lightGreenImageData.left + profiles[profileIndex].widgetOffsetX, lightGreenImageData.top + profiles[profileIndex].widgetOffsetY);
			spriteList[lightGreenImageData.spriteIndex].setSize(lightGreenImageData.width, lightGreenImageData.height);
			spriteList[lightGreenImageData.spriteIndex].render(dev, colorList[lightGreenImageData.colorIndex], 100);
		}
	}
}


	std::string WidgetSessionStatus::getTimeAsString(double seconds, bool forceAllFields, bool padHighest, bool showMillis, bool showFullMillis) {
		if ( showMillis )
            seconds = floor(0.5 + seconds * 1000 ) / 1000;
        else
            seconds = floor(0.5 + seconds );
        
		std:string str = "";

		if ( seconds < 0 )
        {
			str.append("-");
            seconds *= -1;
        }
        
        int hours = (int)( seconds / 3600 );
        
        if ( ( hours > 0 ) || forceAllFields )
        {
            if ( padHighest ) {
				if (hours < 10) {
					str.append("0");
				}
			}
			str.append(std::to_string(static_cast<long long>(hours)));
			str.append(":");
        }
        
        int minutes = ((int) ( seconds / 60 )) % 60 ;
        
        if ( ( hours > 0 ) || ( minutes > 0 ) || forceAllFields )
        {
            if ( ( hours > 0 ) || padHighest ) {
				if (minutes < 10) {
					str.append("0");
				}
			}
			str.append(std::to_string(static_cast<long long>(minutes)));
			str.append(":");
        }
        
        int restSeconds = showMillis ? ((int)floor(seconds)) % 60 : ((int)(floor(0.5 + seconds))) % 60 ;
        
        if ( ( hours > 0 ) || ( minutes > 0 ) || padHighest ) {
            if (restSeconds < 10) {
					str.append("0");
			}
		}
		str.append(std::to_string(static_cast<long long>(restSeconds)));
        
        if ( showMillis )
        {
            int millis = showFullMillis ? ( (int)floor( 0.5 + seconds * 1000 )) % 1000  : ((int) floor( 0.5 + seconds * 10 )) % 10 ;
			str.append(".");
            
            if ( showFullMillis ) {
				if (millis < 10) {
					str.append("00");
				} else if (millis < 100) {
					str.append("0");
				}
			}
			str.append(std::to_string(static_cast<long long>(millis)));
        }
		return str;
	}


WidgetSessionStatus::~WidgetSessionStatus(void)
{

}
